<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if (!isset($is_guest))
{
    ?>			
    <table class='main' cellspacing='0' cellpadding='4'>
    <tr class='head'>
    <td class='head'><?= $language['Register title_error'] ?></td>
    <td class='head'align='right'></td>
    </tr>
    <tr><td class="con1" colspan='2'><?= $language['Register user_register_error'] ?></td>
    </tr>
    </table>
    <?
}
else
{

//-----------------------------------------------------------------
//  Add the new user to the database
//-----------------------------------------------------------------

switch($a)
{
    //-----------------------------------------------------------------
    //  Do register
    //-----------------------------------------------------------------
    
    case 1:
    
        //-----------------------------------------------------------------
        //  Checks and overchecks
        //-----------------------------------------------------------------
        	
        // Konum - Added error warnings
        
        $message = NULL;
        
        if (!isset($_SESSION['terms_agreed']))
        {
            $message = $language['Register terms_agreed_error'];
        }
        else if ($confirm != $language['Captcha answer'])
        {
            $message = $language['Captcha error'];
        }
        else if (!$_POST['email'])
        {
            $message = $language['Register email_empty_error'];
        }
        else if (!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$',
                 $_POST['email']))
        {
            $message = $language['Register email_invalid_error'];
        }
        else if (!$_POST['username'])
        {
            $message = $language['Register name_error'];
        }
        else if (!$_POST['password'])
        {
            $message = $language['Register password_error'];
        }
        else if ($_POST['password2'] != $_POST['password'])
        {
            $message = $language['Register password_match_error'];
        }
        else
        {
            if ($_POST['day'] == 0 || $_POST['month'] == 0 || $_POST['year'] == 0)
            {
                $age = 0;
            }
            else $age = mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']);
            
            //-----------------------------------------------------------------
            //  MYSQL checks
            //-----------------------------------------------------------------
            
            $check_user = $db->getLine("SELECT id FROM users WHERE username = ".
                                       "'{$_POST['username']}'");
            $check_email = $db->getLine("SELECT id FROM users WHERE email = ".
                                        "'{$_POST['email']}'");
            
            if ($check_email != FALSE)
            {
                $message = $language['Register email_exists_error'];
            }
            else if ($check_user != FALSE)
            {
                $message = $language['Register name_exists_error'];
            }
        }
        
        //-----------------------------------------------------------------
        //  Konum: Error display
        //-----------------------------------------------------------------
        
        if (isset($message))
        {
            $title = $language['Register title_error'];
        }
        
        //-----------------------------------------------------------------
        //  Begin writing the information
        //-----------------------------------------------------------------
        
        else
        {
            $info['authorization'] = 1;
            $info['username'] = jcm_htmlspecialchars(jcm_stripslashes
                                ($_POST['username']));
            $info['password'] = md5($_POST['password']);
            $info['email'] = jcm_htmlspecialchars(jcm_stripslashes
                             ($_POST['email']));
            $info['url'] = jcm_htmlspecialchars(jcm_stripslashes
                           ($_POST['url']));
            $info['age'] = $age;
            $info['flag'] = $_POST['flag'];
            $info['location'] = jcm_htmlspecialchars(jcm_stripslashes
                                ($_POST['location']));
            $info['registered'] = time();
            $info['lastvisit'] = time();
            $info['forum_lastvisit'] = time();
            $info['ip'] = find_ip();
            $info['forumposts'] = 0;
            $info['hideemail'] = $_POST['hideemail'];
            $info['calendarbday'] = $_POST['calendarbday'];
            
            if ($config['verify_reg'] == 1)
            {
                $info['status'] = "activate";
                $info['multikey'] = generate_code(20);
                $info['actime'] = time();
            }
            else
            {
                $info['status'] = "ready";
                $info['multikey'] = NULL;
                $info['actime'] = NULL;
            }
            
            $db->insertRow("users", $info);
            
            //-----------------------------------------------------------------
            //  Send mail if the verification is on
            //-----------------------------------------------------------------
            
            if($config['verify_reg'] == 1)
            {
                $info['reason'] = "activate";
                send_email($info);
            }
            
            //-----------------------------------------------------------------
            //  Add birthday to event if 1 was selected
            //-----------------------------------------------------------------
            
            if($_POST['calendarbday'] && $age)
            {
                $get_id = $db->getLine("SELECT id, username FROM users WHERE ".
                                       "username = '{$_POST['username']}' AND ".
                                       "email = '{$_POST['email']}'");
                
                $event_db_add['type'] = "bday";
                $event_db_add['user_id'] = $get_id['id'];
                $event_db_add['user_user'] = $get_id['username'];
                $event_db_add['day'] = $_POST['day'];;
                $event_db_add['month'] = $_POST['month'];
                $event_db_add['year'] = date("Y");
                $event_db_add['message'] = $get_id['username'].
                                           $language['Register birthday'];
                
                $db->insertRow("events",$event_db_add);
            }
            
            $title = $language['Register title_success'];
            $message = $language['Register success'];
        }
        
        //-----------------------------------------------------------------
        //  Redirect + Trick
        //-----------------------------------------------------------------
        
        ?>			
        <table class='main' cellspacing='0' cellpadding='4'>
        <tr class='head'>
        <td class='head'><?= $title ?></td>
        <td class='head'align='right'></td>
        </tr>
        <tr><td class="con1" colspan='2'><?= $message ?></td>
        </tr>
        </table>
        <?
        
    break;
    
    //-----------------------------------------------------------------
    //  Do activation
    //-----------------------------------------------------------------
    
    case 2:
    
        //-----------------------------------------------------------------
        //  Check if the provided key exists and activate the account
        //-----------------------------------------------------------------
        
        $check_key = $db->getLine("SELECT id FROM users WHERE multikey='$key'");
        
        if($check_key)
        {
            //-----------------------------------------------------------------
            //  Update user to ready
            //-----------------------------------------------------------------
            
            $info['status'] = "ready";
            $info['multikey'] = NULL;
            $info['actime'] = NULL;
            
            $db->updateRow("users", $info, "id = {$check_key['id']}");
            
            //-----------------------------------------------------------------
            //  Redirect when finished
            //-----------------------------------------------------------------
            
            $title = $language['Activate title_success'];
            $message = $language['Activate success'];
        }
        else
        {
            $title = $language['Activate title_error'];
            $message = $language['Activate code_error'];
        }
        
        ?>			
        <table class='main' cellspacing='0' cellpadding='4'>
        <tr class='head'>
        <td class='head'><?= $title ?></td>
        <td class='head'align='right'></td>
        </tr>
        <tr><td class="con1" colspan='2'><?= $message ?></td>
        </tr>
        </table>
        <?
        
    break;
    
    //-----------------------------------------------------------------
    //  Do forget password
    //-----------------------------------------------------------------
    
    case 3:
    
        switch($d)
        {
            //-----------------------------------------------------------------
            //  Upadte the user to status forgotten
            //-----------------------------------------------------------------
            
            case 1:
            
                //-----------------------------------------------------------------
                //  Check if the user, email are a match and send the mail + Trick
                //-----------------------------------------------------------------
                
                $check_info = $db->getLine("SELECT * FROM users WHERE username = ".
                                           "'{$_POST['username']}' AND email = ".
                                           "'{$_POST['email']}'");
                
                if (!$check_info)
                {
                    $message = $language['Forgotten error']; 
                }
                
                //-----------------------------------------------------------------
                //  Check if user is not ready or banned
                //-----------------------------------------------------------------
                
                else if ($check_info['status'] == "activate")
                {
                    $message = $language['Activate activation_error'];
                }
                else if ($check_info['status'] == "banned")
                {
                    $message = $language['Bans error'];
                }
                
                // Check if this case could be possible
                /*else if ($check_info['status'] == "invisible")
                {
                    $message = $language['i=?'];
                }*/
                  
                //-----------------------------------------------------------------
                //  If the user is ready or forgot send him the email
                //-----------------------------------------------------------------
                
                if (isset($message))
                {
                    $title = $language['Forgotten title_error'];
                }
                else
                {
                    //-----------------------------------------------------------------
                    //  Update status to forgot password
                    //-----------------------------------------------------------------
                    
                    $check_info['status'] = "forgot";
                    $check_info['multikey'] = generate_code(20);
                    $check_info['actime'] = time();
                    
                    $db->updateRow("users", $check_info, "id = {$check_info['id']}");
                    
                    //-----------------------------------------------------------------
                    //  Send Mail
                    //-----------------------------------------------------------------
                    
                    $check_info['reason'] = "forgot";
                    send_email($check_info);
                    
                    //-----------------------------------------------------------------
                    //  Redirect
                    //-----------------------------------------------------------------
                    
                    $title = $language['Forgotten title_success'];
                    $message = $language['Forgotten email_success'];
                }
                
                ?>			
                <table class='main' cellspacing='0' cellpadding='4'>
                <tr class='head'>
                <td class='head'><?= $title ?></td>
                <td class='head'align='right'></td>
                </tr>
                <tr><td class="con1" colspan='2'><?= $message ?></td>
                </tr>
                </table>
                <?
                
            break;
            
            //-----------------------------------------------------------------
            //  Change the password and make the user ready
            //-----------------------------------------------------------------
            
            case 2:
            
                //-----------------------------------------------------------------
                //  Check if the provided key exists and modify the pass
                //-----------------------------------------------------------------
                
                $check_key = $db->getLine("SELECT id, status FROM users WHERE ".
                                          "multikey = '".addslashes($key)."'");
                                          
                if (!$check_key)
                {
                    $title = $language['Forgotten title_error'];
                    $message = $language['Forgotten key_error']; 
                }
                else
                {
                    //-----------------------------------------------------------------
                    //  Check what kind of user was he + Update db + redirect
                    //-----------------------------------------------------------------
                    
                    $info['password'] = md5($password);
                    $info['status'] = "ready";
                    $info['multikey'] = NULL;
                    $info['actime'] = NULL;
                    
                    $db->updateRow("users", $info, "id = {$check_key['id']}");
                    
                    $title = $language['Forgotten title_success'];
                    $message = $language['Forgotten success'];
                }
                
                ?>			
                <table class='main' cellspacing='0' cellpadding='4'>
                <tr class='head'>
                <td class='head'><?= $title ?></td>
                <td class='head'align='right'></td>
                </tr>
                <tr><td class="con1" colspan='2'><?= $message ?></td>
                </tr>
                </table>
                <?
                
            break;
            
            //-----------------------------------------------------------------
            //  Change the password Form
            //-----------------------------------------------------------------
            
            case 3:
            
                //-----------------------------------------------------------------
                //  Check if the provided key exists and show form
                //-----------------------------------------------------------------
                
                $check_key = $db->getLine("SELECT id FROM users WHERE multikey = '".
                                  addslashes($key)."'");
                                  
                if($check_key)
                {
                    ?>
                    <form method="post" action="<?= $PHP_SELF ?>?n=modules/register">
                    <table class="main" cellspacing="1" cellpadding="4" width="100%">
                    <tr class="head"> 
                    <td colspan="2" class="head"><?= $language['Forgotten title'] ?></td>
                    </tr>
                    <tr>
                    <td class="con1" width="42%" valign="middle"><span class=
                    "hilight"><?= $language['Forgotten desc3'] ?></span></td>
                    <td class="con1" width="58%" valign="bottom"><input type=
                    "password" name="password" style="width:100%" maxlength=
                    "25" class="box" tabindex="5"></td>
                    </tr>
                    <tr>
                    <td class="con1"><input type="button" style="width:100"
                    class="box" value="<?= $language['Forgotten desc4'] ?>"
                    onClick="window.history.go(-1);" tabindex="8"></td>
                    <td class="con1" align="right"><input type="submit" style=
                    "width:100" class="box" value="<?= $language['Forgotten desc5'] ?>"
                    tabindex="7"></td>
                    </tr>
                    <input type="hidden" name="a" value="3">
                    <input type="hidden" name="d" value="2">
                    <input type="hidden" name="key" value="<?= $key ?>">
                    </table></form>
                    <?
                }
                else
                {
                    ?>			
                    <table class='main' cellspacing='0' cellpadding='4'>
                    <tr class='head'>
                    <td class='head'><?= $language['Forgotten title_error'] ?></td>
                    <td class='head'align='right'></td>
                    </tr>
                    <tr><td class="con1" colspan='2'><?=
                         $language['Forgotten key_error'] ?></td>
                    </tr>
                    </table>
                    <?
                }
                
            break;
            
            //-----------------------------------------------------------------
            //  Show form for lost password
            //-----------------------------------------------------------------
            
            default:
            
                ?>
                <form method="post" action="<?= $PHP_SELF ?>?n=modules/register">
                <table class="main" cellspacing="1" cellpadding="4" width="100%">
                <tr class="head"> 
                <td colspan="2" class="head"><?= $language['Forgotten title'] ?></td>
                </tr>
                <tr>
                <td class="con1" width="42%" valign="middle"><span class=
                "hilight"><?= $language['Forgotten desc'] ?></span></td>
                <td class="con1" width="58%" valign="bottom"><input type="text"
                name="username" style="width:100%" maxlength="25" class="box"
                tabindex="5"></td>
                </tr>
                <tr>
                <td class="con1" valign="middle"><span class="hilight"><?= 
                $language['Forgotten desc2'] ?></span></td>
                <td class="con1" valign="bottom"><input type="text" name="email"
                style="width:100%" maxlength="35" class="box" tabindex="6"></td>
                </tr>
                <tr>
                <td class="con1"><input type="button" style="width:100"
                class="box" value="<?= $language['Forgotten desc4'] ?>"
                onClick="window.history.go(-1);" tabindex="8"></td>
                <td class="con1" align="right"><input type="submit"
                style="width:100" class="box" value="<?= 
                $language['Forgotten desc5'] ?>" tabindex="7"></td>
                </tr>
                <input type="hidden" name="a" value="3">
                <input type="hidden" name="d" value="1">
                </table></form>
                <?
        }
        
    break;
    
    //-----------------------------------------------------------------
    //  Show register form
    //-----------------------------------------------------------------
    
    default:
    
        //-----------------------------------------------------------------
        //  Check if the terms exists
        //-----------------------------------------------------------------
        
        if ($config['terms_tg'] == 0)
        {
            $_SESSION['terms_agreed'] = 1;
        }
        //-----------------------------------------------------------------
        //  Display the register form
        //-----------------------------------------------------------------
        
        if(isset($_SESSION['terms_agreed']))
        {
            ?>
            <form method="post" action="<?= $PHP_SELF ?>?n=modules/register">
            <table class="main" cellspacing="1" cellpadding="4" width="100%">
            <col width="40%"><col width="60%">
            <tr class="head"> 
            <td colspan="2" class="head"><?= $language['Register title'] ?></td>
            </tr>
            <tr class="bottom">
            <td height="21" colspan="2"><?= $language['Register title2'] ?></td>
            </tr>
            
            <tr>
            <td class="con1" valign="middle"><span class="hilight"><?= 
                $language['Register desc'] ?></span></td>
            <td class="con1" valign="bottom"><input type="text" name="username"
                style="width:100%" maxlength="25" class="box" tabindex="5"></td>
            </tr>
            
            <tr>
            <td class="con1" valign="middle"><span class="hilight"><?=
                $language['Register desc2'] ?></span></td>
            <td class="con1" valign="bottom"><input type="password" name="password"
                style="width:100%" maxlength="25" class="box" tabindex="6"></td>
            </tr>
            
            <tr>
            <td class="con1" valign="middle"><span class="hilight"><?=
                $language['Register desc3'] ?></span></td>
            <td class="con1" valign="bottom"><input type="password" name="password2"
                style="width:100%" maxlength="25" class="box" tabindex="7"></td>
            </tr>
            
            <tr>
            <td class="con1" valign="middle"><span class="hilight"><?=
                $language['Register desc4'] ?></span></td>
            <td class="con1" valign="bottom"><input type="text" name="email"
                style="width:100%" maxlength="35" class="box" tabindex="8"></td>
            </tr>
            
            <?//---------- Optional fields -----------?>
            
            <tr class="bottom">
            <td height="21" colspan="2"><?= $language['Register title3'] ?></td>
            </tr>
            
            <tr>
            <td class="con1" valign="middle"><?= $language['Register desc5'] ?></td>
            <td class="con1" valign="bottom"><input type="text" name="url"
                style="width:100%" maxlength="35" class="box" tabindex="9"></td>
            </tr>
            
            <tr>
            <td class="con1" valign="middle"><?= $language['Register desc6'] ?>
            <?
            if (isset($o))
            {?>
                <a href="<?= $PHP_SELF ?>?n=modules/register"><?= $language['Register desc7'] ?></a></td>
                <td class='con1' valign='bottom'><input type='text' name='location'
                style='width:100%' maxlength='35' class='box' tabindex='10'></td>
                
                <input type="hidden" name="flag" value="none">
            <?}
            else
            {?>
                <a href="<?= $PHP_SELF ?>?n=modules/register&o=1"><?= $language['Register desc8'] ?></a></td>
                <td class='con1' valign='bottom'><select name='flag' style='width:100%'
                class='box' tabindex='10'><?= find_country() ?></select></td>
                
                <input type="hidden" name="location" value="NULL">
            <?
            }?>
            </tr>
            
            <tr>
            <td class="con1" valign="middle"><?= $language['Register desc9'] ?></td>
            <td class="con1" valign="bottom">
            <table cellspacing="0" cellpadding="0" width="100%">
            <tr class='empty'>
            <td width='30%'><select name="day" class="box" style="width:100%"
            tabindex='11'><option value='0'></option>
            <?
                for ($i = 1; $i <= 31; $i++)
                    echo "<option value='$i'>$i</option>\n"
            ?>
            </select></td>
            <td align='center' width='5%'>-</td>
            <td width='30%'><select name="month" class="box" style="width:100%"
                tabindex='12'><option value='0'></option>
            <?
                for ($i = 1; $i <= 12; $i++)
                    echo "<option value='$i'>{$months[$i]}</option>\n"
            ?>
            </select></td>
            <td align='center' width='5%'>-</td>
            <td width='30%'><select name="year" class="box" style="width:100%"
                tabindex='13'><option value='0'></option>
            <?
                for ($i = 1971; $i <= date("Y") - 5; $i++)
                    echo "<option value='$i'>$i</option>\n"
            ?>
            </select></td>
            </tr>
            </table>
            </td>
            </tr>
            
            <tr>
            <td class="con1" valign="middle"><?= $language['Register desc10'] ?></td>
            <td class="con1" valign="bottom"><input type="radio" name="hideemail"
                value="1" tabindex='14'>&nbsp;<?= $language['Register desc14'] 
                ?>&nbsp;&nbsp;&nbsp;<input type="radio" name="hideemail" value="0"
                checked tabindex='15'>&nbsp;<?= $language['Register desc15'] ?></td>
            </tr>
            
            <tr>
            <td class="con1" valign="middle"><?= $language['Register desc11'] ?></td>
            <td class="con1" valign="bottom"><input type="radio" name="calendarbday"
                value="1" checked tabindex='16'>&nbsp;<?= $language['Register desc14']
                ?>&nbsp;&nbsp;&nbsp;<input type="radio" name="calendarbday" value="0"
                tabindex='17'>&nbsp;<?= $language['Register desc15'] ?></td>
            </tr>
            	
            <tr class='con1'>
            <td valign='top'><span style='color:green;'><?= $language['Captcha question']
            ?></span></td>
            <td><input name="confirm" type="text" id="confirm" size="8" maxlength="8"
                class='box' tabindex='18'></td>
            </tr>
            
            <tr>
            <td class="con1"><input type="button" style="width:100" class="box"
                value="<?= $language['Register desc12'] ?>"
                onClick="window.history.go(-1);" tabindex="20"></td>
            <td class="con1" align="right"><input type="submit" style="width:100"
                class="box" value="<?= $language['Register desc13'] ?>" tabindex="19"></td>
            </tr>
            
            <input type="hidden" name="a" value="1">
            </table>
            </form>
            <?
        }
        else
        {
            switch($d)
            {
                //-----------------------------------------------------------------
                //  Deploy value to terms_agreed
                //-----------------------------------------------------------------
                
                case 1:
                
                    //-----------------------------------------------------------------
                    //  Do it and redirect
                    //-----------------------------------------------------------------
                    
                    $_SESSION['terms_agreed'] = 1;
                    header("location: $PHP_SELF?n=modules/register");
                    
                break;
                
                default:
                
                    //-----------------------------------------------------------------
                    //  Show Terms and Conditions
                    //-----------------------------------------------------------------
                    
                    ?>
                    <form method="post" action="<?= $PHP_SELF ?>?n=modules/register&d=1">
                    <table class="main" cellspacing="1" cellpadding="4" width="100%">
                    <col width="50%"><col width="50%">
                    <tr class="head"> 
                    <td colspan="2" class="head"><?= $language['Register title'] ?></td>
                    </tr>
                    <tr class="con1"> 
                    <td colspan="2"><?= parse_message($config['terms_txt']) ?></td>
                    </tr>
                    <tr class="con1"> 
                    <td align="right"><input type="button" style="width:100"
                        class="box" value="<?= $language['Register desc16'] ?>"
                        onClick="window.history.go(-1);"></td>
                    <td align="left"><input type="submit" style="width:100"
                        class="box" value="<?= $language['Register desc17'] ?>"></td>
                    </tr>
                    </form>
                    </table>
                    <?
            }
        }
}
}

?>
